package com.igogogo.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.igogogo.tbkrequest.TbkKeyWordRequest;
import com.igogogo.tbkrequest.TbkRequest;
import com.igogogo.tbkresponse.TbkItemResponse;
import com.igogogo.utils.ResultJson;
import com.igogogo.utils.TbkUtils;
import com.igogogo.utils.WordUtils;
import com.taobao.api.request.JuItemsSearchRequest.TopItemQuery;
import com.taobao.api.request.TbkDgItemCouponGetRequest;
import com.taobao.api.request.TbkItemGetRequest;
import com.taobao.api.request.TbkItemInfoGetRequest;
import com.taobao.api.request.TbkItemRecommendGetRequest;
import com.taobao.api.request.TbkJuTqgGetRequest;
import com.taobao.api.request.TbkShopGetRequest;
import com.taobao.api.request.TbkShopRecommendGetRequest;
import com.taobao.api.request.TbkTpwdCreateRequest;
import com.taobao.api.response.JuItemsSearchResponse;
import com.taobao.api.response.TbkCouponGetResponse;
import com.taobao.api.response.TbkDgItemCouponGetResponse;
import com.taobao.api.response.TbkDgItemCouponGetResponse.TbkCoupon;
import com.taobao.api.response.TbkItemGetResponse;
import com.taobao.api.response.TbkItemInfoGetResponse;
import com.taobao.api.response.TbkItemRecommendGetResponse;
import com.taobao.api.response.TbkJuTqgGetResponse;
import com.taobao.api.response.TbkShopGetResponse;
import com.taobao.api.response.TbkShopRecommendGetResponse;
import com.taobao.api.response.TbkSpreadGetResponse;
import com.taobao.api.response.TbkTpwdCreateResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "tbk")
@Api(value = "TbkController", description = "淘宝客相关接口")
public class TbkController {

	@ApiOperation(value = "通过商品名称获取商品信息")
	@RequestMapping(value = "query", method = RequestMethod.POST)
	public ResultJson query(@RequestBody TbkKeyWordRequest request) {
		ResultJson json = new ResultJson();
		TbkDgItemCouponGetRequest itemrequest = new TbkDgItemCouponGetRequest();
		itemrequest.setQ(request.getKeyword());
		TbkDgItemCouponGetResponse result = TbkUtils.queryItemCoupon(itemrequest);
		if (result != null) {
			List<TbkItemResponse> itemResponseList = new ArrayList<TbkItemResponse>();
			List<TbkCoupon> tbkCoupons = result.getResults();
			if (tbkCoupons != null && tbkCoupons.size() > 0) {
				for (TbkCoupon tbkCoupon : tbkCoupons) {
					TbkItemResponse item = new TbkItemResponse();
					// 短地址
					TbkRequest urls = new TbkRequest();
					urls.setUrls(new String[] { tbkCoupon.getCouponClickUrl() });
					TbkSpreadGetResponse querySpread = TbkUtils.querySpread(urls);
					if (querySpread != null && querySpread.getResults() != null
							&& querySpread.getResults().size() > 0) {
						item.setShortUrl(querySpread.getResults().get(0).getContent());
					}
					// 优惠券
					TbkRequest tbkRequest = new TbkRequest();
					tbkRequest.setMe(WordUtils.meargs(tbkCoupon.getCouponClickUrl()));
					TbkCouponGetResponse queryCoupon = TbkUtils.queryCoupon(tbkRequest);
					if (queryCoupon != null && queryCoupon.getData() != null) {
						item.setCouponAmount(queryCoupon.getData().getCouponAmount());
						item.setCouponStartTime(queryCoupon.getData().getCouponStartTime());
						item.setCouponEndTime(queryCoupon.getData().getCouponEndTime());
					}

					item.setCoupon(tbkCoupon);
					itemResponseList.add(item);
				}
			}
			json.setResult(itemResponseList);
		}
		json.setMsgcode(200);
		json.setMsginfo("success");
		return json;
	}

	@ApiOperation(value = "聚划算商品搜索接口  [taobao.ju.items.search]")
	@RequestMapping(value = "jhs", method = RequestMethod.POST)
	public ResultJson queryJHS(@RequestBody TopItemQuery request) {
		ResultJson json = new ResultJson();
		JuItemsSearchResponse result = TbkUtils.queryJHS(request);
		if (result != null) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(result.getResult());
		}
		return json;
	}

	@ApiOperation(value = "阿里妈妈推广券信息查询(带券ID与商品ID的加密串)  [taobao.tbk.coupon.get]")
	@RequestMapping(value = "coupon", method = RequestMethod.POST)
	public ResultJson queryCoupon(@RequestBody TbkRequest request) {
		ResultJson json = new ResultJson();
		TbkCouponGetResponse result = TbkUtils.queryCoupon(request);
		if (result != null) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(result.getData());
		}
		return json;
	}

	@ApiOperation(value = "淘宝客淘口令 [taobao.tbk.tpwd.create]")
	@RequestMapping(value = "tpwd", method = RequestMethod.POST)
	public ResultJson queryTpwdCreate(@RequestBody TbkTpwdCreateRequest request) {
		ResultJson json = new ResultJson();
		TbkTpwdCreateResponse result = TbkUtils.queryTpwdCreate(request);
		if (result != null) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(result.getData());
		}
		return json;
	}

	@ApiOperation(value = "物料传播方式获取 [taobao.tbk.spread.get]")
	@RequestMapping(value = "spread", method = RequestMethod.POST)
	public ResultJson querySpread(@RequestBody TbkRequest request) {
		ResultJson json = new ResultJson();
		TbkSpreadGetResponse result = TbkUtils.querySpread(request);
		if (result != null) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(result.getResults());
		}
		return json;
	}

	@ApiOperation(value = "淘抢购api [taobao.tbk.ju.tqg.get]")
	@RequestMapping(value = "tqg", method = RequestMethod.POST)
	public ResultJson queryTqg(@RequestBody TbkJuTqgGetRequest request) {
		ResultJson json = new ResultJson();
		TbkJuTqgGetResponse result = TbkUtils.queryTqg(request);
		if (result != null) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(result.getResults());
		}
		return json;
	}

	@ApiOperation(value = "淘宝客店铺关联推荐查询 [taobao.tbk.shop.recommend.get]")
	@RequestMapping(value = "shoprecommend", method = RequestMethod.POST)
	public ResultJson queryShopRecommend(@RequestBody TbkShopRecommendGetRequest request) {
		ResultJson json = new ResultJson();
		TbkShopRecommendGetResponse result = TbkUtils.queryShopRecommend(request);
		if (result != null) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(result.getResults());
		}
		return json;
	}

	@ApiOperation(value = "淘宝客店铺查询 [taobao.tbk.shop.get]")
	@RequestMapping(value = "shop", method = RequestMethod.POST)
	public ResultJson queryShop(@RequestBody TbkShopGetRequest request) {
		ResultJson json = new ResultJson();
		TbkShopGetResponse result = TbkUtils.queryShop(request);
		if (result != null) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(result.getResults());
		}
		return json;
	}

	@ApiOperation(value = "淘宝客商品关联推荐查询 [taobao.tbk.item.recommend.get]")
	@RequestMapping(value = "itemrecommend", method = RequestMethod.POST)
	public ResultJson queryItemRecommend(@RequestBody TbkItemRecommendGetRequest request) {
		ResultJson json = new ResultJson();
		TbkItemRecommendGetResponse result = TbkUtils.queryItemRecommend(request);
		if (result != null) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(result.getResults());
		}
		return json;
	}

	@ApiOperation(value = "淘宝客商品详情简版 [taobao.tbk.item.info.get]")
	@RequestMapping(value = "iteminfo", method = RequestMethod.POST)
	public ResultJson queryItemInfo(@RequestBody TbkItemInfoGetRequest request) {
		ResultJson json = new ResultJson();
		TbkItemInfoGetResponse result = TbkUtils.queryItemInfo(request);
		if (result != null) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(result.getResults());
		}
		return json;
	}

	@ApiOperation(value = "淘宝客商品查询 [taobao.tbk.item.get]")
	@RequestMapping(value = "item", method = RequestMethod.POST)
	public ResultJson queryItem(@RequestBody TbkItemGetRequest request) {
		ResultJson json = new ResultJson();
		TbkItemGetResponse result = TbkUtils.queryItem(request);
		if (result != null) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(result.getResults());
		}
		return json;
	}

	@ApiOperation(value = "好券清单API导购 [taobao.tbk.dg.item.coupon.get]")
	@RequestMapping(value = "itemcoupon", method = RequestMethod.POST)
	public ResultJson queryItemCoupon(@RequestBody TbkDgItemCouponGetRequest request) {
		ResultJson json = new ResultJson();
		TbkDgItemCouponGetResponse result = TbkUtils.queryItemCoupon(request);
		if (result != null) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(result.getResults());
		}
		return json;
	}

}
